package ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.GoodOrErrorQuestionBean;
import bean.QuestionBean;
import bean.ReviewAnswerBean;
import jp.wasabeef.richeditor.RichEditor;
import student.andlei.com.student.R;
import utils.GsonUtils;
import view.BottomAnswerPopWindow;
import view.ConfirmPopWindow;

/**
 * 好题，错题本 - 详情
 */
public class GoodOrErrorQuestionDetailActivity extends BaseNetActivity {
    private TextView tv_number,
            tv_date_time,
            img_open,
            img_delete,
            tv_answer;
    private RichEditor   tv_type_content;
    private ImageView tv_state,
            tv_last,
            tv_next;
    private Button
            bt_strengtheningpractice,
            bt_lookanswer,
            bt_finish,
            bt_nofinish;
    private int i = 0;
    private View layoutview;
    private List<GoodOrErrorQuestionBean.mList> list = new ArrayList<>();
    private int book_id;
    private int question_id;
    private int total;
    private QuestionBean questionBean;
    private int GoodOrMattertype;
    private boolean isNext;
    private String date;
    private int type_id,
                question_status,subject_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (baseBean.code == 200) {
            if (action.equals("Book/view")) {
                questionBean = (QuestionBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,QuestionBean.class);
                question_id = questionBean.question_id;
                book_id = questionBean.book_id;
                updateUi(questionBean);

            } else if (action.equals("Book/next")) {
                questionBean = (QuestionBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,QuestionBean.class);
                question_id = questionBean.question_id;
                book_id = questionBean.book_id;
                if(isNext){
                    i++;
                }else {
                    i--;
                }
                updateUi(questionBean);
            }else if(action.equals("Book/annihilate")){
                loadData();
            }else if(action.equals("Book/reviewAnswer")){
                ReviewAnswerBean answerBean = (ReviewAnswerBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,ReviewAnswerBean.class);
                String slore ="";
                if(questionBean == null){return;}
                for(QuestionBean.Lore lore : questionBean.lore){
                    slore+=lore.lore_content+",";
                }
                new BottomAnswerPopWindow(this, ""+answerBean.answer, ""+questionBean.answer, ""+questionBean.analysis, ""+slore).showAtBottom(layoutview);
            }
            else if(action.equals("Book/remove")){
                if(total <= 1){
                    finish();

                }else {
                    if(i<=0){
                        NextData(2);
                        i++;
                    }else if(0<i && i<(total-1)){
                        NextData(2);
                        i++;
                    }
                    else if(i >= (total-1)){
                        NextData(1);
                        i--;
                    }
                    total--;
                }
                toast("删除成功");
            }
        }else {
            toast(baseBean.msg);
        }

    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_goodorerrorquestion_detail, null);
        return R.layout.activity_goodorerrorquestion_detail;
    }

    @Override
    protected void findViews() {
        tv_number = getView(R.id.tv_number);
        tv_date_time = getView(R.id.tv_date_time);
        tv_type_content = getView(R.id.tv_type_content);
        tv_type_content.setInputEnabled(false);
        tv_type_content.setInitialScale(200);
        tv_state = getView(R.id.tv_state);
        tv_answer = getView(R.id.tv_answer);
        tv_last = getView(R.id.tv_last);
        tv_next = getView(R.id.tv_next);
        img_open = getView(R.id.img_open);
        img_delete = getView(R.id.img_delete);

        bt_strengtheningpractice = getView(R.id.bt_strengtheningpractice);
        bt_lookanswer = getView(R.id.bt_lookanswer);
        bt_finish = getView(R.id.bt_finish);
        bt_nofinish = getView(R.id.bt_nofinish);
    }

    @Override
    protected void formatViews() {
        img_delete.setOnClickListener(this);
        img_open.setOnClickListener(this);
        bt_lookanswer.setOnClickListener(this);
        bt_strengtheningpractice.setOnClickListener(this);
        bt_finish.setOnClickListener(this);
        bt_nofinish.setOnClickListener(this);
        tv_last.setOnClickListener(this);
        tv_next.setOnClickListener(this);

    }

    @Override
    protected void formatData() {
//        for (int i = 0; i < 10; i++) {
//            list.add("状态值" + i);
//        }
        loadData();
    }
    //加载题目详情
    private void loadData() {
        Map<String, String> map = new HashMap<>();
        map.put("book_id", book_id + "");
        post("Book/view", map, BaseBean.class, false);
    }
    //上一题 下一题
    private void NextData(int jump){
        Map<String,String> map = new HashMap<>();
        map.put("subject_id",subject_id+"");
        map.put("book_id",""+book_id);
        map.put("date",date);
        map.put("type_id",type_id+"");
        map.put("question_status",question_status+"");
        map.put("type",""+GoodOrMattertype);
        map.put("jump",jump+"");//1:上一题 2:下一题（默认）
        post("Book/next",map,BaseBean.class,false);
    }
    //消灭 取消消灭
    private void  perishData(int type){
        Map<String,String> map = new HashMap<>();
        map.put("book_id",""+book_id);
        map.put("type",type+"");//1:已消灭 0:未消灭
        post("Book/annihilate",map,BaseBean.class,true);
    }
    //移除题目
    private void DeleteData(){
        Map<String, String> map = new HashMap<>();
        map.put("book_id", book_id + "");
        post("Book/remove", map, BaseBean.class, false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        //type 1:好题
        GoodOrMattertype =  bundle.getInt("type");
        if(GoodOrMattertype == 1){
            bt_finish.setVisibility(View.GONE);
            bt_nofinish.setVisibility(View.GONE);
            tv_state.setVisibility(View.GONE);
            setTitle(bundle.getString("subject_name")+"好题本");
        }else {
            bt_finish.setVisibility(View.VISIBLE);
            bt_nofinish.setVisibility(View.VISIBLE);
            tv_state.setVisibility(View.VISIBLE);
            setTitle(bundle.getString("subject_name")+"错题本");
        }
        date =   bundle.getString("date");
        type_id =  bundle.getInt("type_id");
        subject_id = bundle.getInt("subject_id");
        question_status = bundle.getInt("question_status");
        total = bundle.getInt("total");
        book_id = bundle.getInt("book_id");
         i = bundle.getInt("position");
        list = (List<GoodOrErrorQuestionBean.mList>) bundle.getSerializable("list");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //草稿纸
            case R.id.img_open:
                new ConfirmPopWindow(this).showAtBottom(layoutview);
                break;
            //删除按钮
            case R.id.img_delete:
                toast("删除按钮");
                DeleteData();
                break;
            //强化练习
            case R.id.bt_strengtheningpractice:
                Bundle bundle = new Bundle();
                bundle.putInt("question_id",question_id);
                jumpTo(StrengtheningPracticeActivity.class,bundle);
                break;
            //查看答案
            case R.id.bt_lookanswer:
                Map<String,Object> map = new HashMap<>();
                map.put("book_id",book_id);
                post("Book/reviewAnswer",map,BaseBean.class,false);
                break;
            //已消灭
            case R.id.bt_finish:
                //1:已消灭 0:未消灭
                perishData(1);
                break;
            //未消灭
            case R.id.bt_nofinish:
                //1:已消灭 0:未消灭
                perishData(0);
                break;
            //上一题
            case R.id.tv_last:
                if (i <= 0) {
                    toast("已是第一题");
                    return;
                }
                isNext = false;
                NextData(1);//1:上一题 2:下一题（默认）
                break;
            //下一题
            case R.id.tv_next:
                if (i >= list.size() - 1) {
                    toast("已是最后一题");
                    return;
                }
                isNext = true;
                NextData(2);//1:上一题 2:下一题（默认）
                break;
        }
    }

    private void updateUi(QuestionBean questionBean) {
        tv_number.setText((i + 1) + "/" + total + "题");
        //1已消灭 0未消灭
        if(questionBean.question_status==0){
            tv_state.setImageResource(R.mipmap.img_weixiaomie);
        }else {
            tv_state.setImageResource(R.mipmap.img_yixiaomie);
        }
        tv_type_content.setHtml(questionBean.question_content);
//        tv_type_content.setText(questionBean.question_content);
        tv_answer.setText(questionBean.question_options);
        tv_date_time.setText(questionBean.create_time);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setResult(0);
    }
}
